package pers.cj.pdos.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.controller.entity.ResponseEntity;
import pers.cj.pdos.controller.entity.ResponseList;
import pers.cj.pdos.entity.BudgetProject;
import pers.cj.pdos.entity.Company;
import pers.cj.pdos.entity.CustomType;
import pers.cj.pdos.service.IBudgetProjectService;
import pers.cj.pdos.utils.DataUtil;
import pers.cj.pdos.utils.Utils;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Controller
@RequestMapping("/budget")
public class BudgetProjectController extends BaseController {
    @Autowired
    private IBudgetProjectService budgetProjectService;

    @RequiresPermissions("budgetProject:query")
    @RequestMapping("/budgetProject")
    public String budgetProject(){
        return "/budget/budgetProject";
    }

    @RequiresPermissions("budgetProject:insert")
    @RequestMapping("/budgetProjectNew")
    public String budgetProjectNew(){
        return "/budget/budgetProjectNew";
    }

    @RequiresPermissions("budgetProject:insert")
    @RequestMapping("/budgetProjectEdit")
    public String budgetProjectEdit(Model model, String id){
        model.addAttribute("id",id);
        return "/budget/budgetProjectEdit";
    }


    @ResponseBody
    @RequiresPermissions("budgetProject:query")
    @RequestMapping("/budgetProject/getList")
    public ResponseList getList(int page, int limit, String searchType,String search){
        Company company = getCompany();
        Integer type = null;
        if (!Utils.isEmpty(searchType)){
            type = Integer.parseInt(searchType);
        }
        List<BudgetProject> list = budgetProjectService.listPage(company.getId(), page,limit,type,search);
        long count = budgetProjectService.count(company.getId(), type,search);
        return ResponseList.success(count,list);
    }

    @ResponseBody
    @RequiresPermissions("budgetProject:insert")
    @RequestMapping("/budgetProject/add")
    public ResponseEntity add(@RequestBody BudgetProject budgetProject) {
        Company company = getCompany();
        if (budgetProjectService.checkName(budgetProject.getName(),company.getId())){
            return new ResponseEntity(2,"名称已存在");
        }
        DataUtil.processEntity(company,budgetProject);
        budgetProjectService.save(budgetProject);
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequiresPermissions("budgetProject:insert")
    @RequestMapping("/budgetProject/delete")
    public ResponseEntity delete(@RequestBody BudgetProject budgetProject) {
        try {
            DataUtil.processEntity(false,getCompany(),budgetProject);
            budgetProjectService.removeById(budgetProject.getId());
            return ResponseEntity.success();
        } catch (Exception e) {
            return ResponseEntity.fail("已被使用无法删除");
        }
    }

    @ResponseBody
    @RequiresPermissions("budgetProject:insert")
    @RequestMapping("/budgetProject/update")
    public ResponseData update(@RequestBody BudgetProject budgetProject) {
        Company company = getCompany();
        BudgetProject old = budgetProjectService.getById(budgetProject.getId());
        if (!old.getName().equals(budgetProject.getName()) && budgetProjectService.checkName(budgetProject.getName(),company.getId())){
            return ResponseData.fail("名称已存在");
        }

        DataUtil.processEntity(false,company,budgetProject);
        budgetProjectService.updateById(budgetProject);
        return ResponseData.success(budgetProject);
    }

    @ResponseBody
    @RequestMapping("/budgetProject/getAllIn")
    public ResponseData getAllIn() {
        Company company = getCompany();
        //返回数据
        return ResponseData.success(budgetProjectService.list(new QueryWrapper<BudgetProject>().eq("type",1).eq("company_id",company.getId())));
    }

    @ResponseBody
    @RequestMapping("/budgetProject/getAllOut")
    public ResponseData getAllOut() {
        Company company = getCompany();
        //返回数据
        return ResponseData.success(budgetProjectService.list(new QueryWrapper<BudgetProject>().eq("type",2).eq("company_id",company.getId())));
    }

    @ResponseBody
    @RequestMapping("/budgetProject/getById")
    public ResponseData getById(String id) {
        BudgetProject budgetProject = budgetProjectService.getById(id);
        if (budgetProject == null) {
            return ResponseData.fail("id不正确");
        }
        //返回数据
        return ResponseData.success(budgetProject);
    }

}
